package com.sls.nzdapplication.ui.custom;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;


import com.sls.nzdapplication.R;


import java.util.ArrayList;
import java.util.List;

public class MapLayer extends RelativeLayout implements View.OnClickListener {
    private CardView defaultMapLayer;

    private CardView satelliteMapLayer;

    private TextView defaultMapTextActive;

    private TextView satelliteMapTextActive;

    private CheckBox substationView;

    private CheckBox cableWellView;

    private CheckBox equipmentView;

    private CheckBox cableLineView;

    private CheckBox channelView;

    private MapLayerClickEvent mapLayerClickEvent;


    public MapLayer(Context context) {
        super(context);
    }

    public MapLayer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MapLayer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.map_layer_layout, this, true);
        initView();
        initListener();
    }

    public void setMapLayerClickEvent(MapLayerClickEvent mapLayerClickEvent){
        if(mapLayerClickEvent == null){
            return;
        }
        this.mapLayerClickEvent = mapLayerClickEvent;

    }

    private void initView(){
        defaultMapLayer = findViewById(R.id.traffic_map_layer);
        satelliteMapLayer = findViewById(R.id.satellite_map_layer);
        defaultMapTextActive = findViewById(R.id.traffic_layer_select);
        satelliteMapTextActive = findViewById(R.id.satellite_layer_select);
        substationView = findViewById(R.id.subsation_checkbox);
        cableWellView = findViewById(R.id.cablewell_checkbox);
        equipmentView = findViewById(R.id.equipment_checkbox);
        cableLineView = findViewById(R.id.cableline_checkbox);
        channelView = findViewById(R.id.channel_checkbox);
//        offLineMap = findViewById(R.id.setting_offline_map);
    }

    private void initListener(){
        defaultMapLayer.setOnClickListener(this);
        satelliteMapLayer.setOnClickListener(this);
        substationView.setOnClickListener(this);
        cableWellView.setOnClickListener(this);
        cableLineView.setOnClickListener(this);
        equipmentView.setOnClickListener(this);
        channelView.setOnClickListener(this);
//        offLineMap.setOnClickListener(this);
    }

    private List<String> getCheckedLayer(){
        List<String> layers = new ArrayList<>();
        if(substationView.isChecked()){
            layers.add("substation");
        }
        if(channelView.isChecked()){
            layers.add("channel");
        }
        if(cableWellView.isChecked()){
            layers.add("cableWell");
        }
        if(equipmentView.isChecked()){
            layers.add("equipment");
        }
        if(cableLineView.isChecked()){
            layers.add("cableLine");
        }
        return layers;
    }

    @Override
    public void onClick(View view) {
        if(view == defaultMapLayer){
            if(mapLayerClickEvent==null){
                return;
            }
            defaultMapTextActive.setVisibility(View.VISIBLE);
            satelliteMapTextActive.setVisibility(View.GONE);
            mapLayerClickEvent.openMapLayer("default");
        }
        else if(view == satelliteMapLayer){
            if(mapLayerClickEvent==null){
                return;
            }
            defaultMapTextActive.setVisibility(View.GONE);
            satelliteMapTextActive.setVisibility(View.VISIBLE);
            mapLayerClickEvent.openMapLayer("satellite");
        }
//        else if(view == offLineMap){
//            mapLayerClickEvent.openofflineMap();
//        }
        else{
            mapLayerClickEvent.openOverLayer(getCheckedLayer());
        }

    }

    public interface MapLayerClickEvent{

        void openMapLayer(String type);

        void openOverLayer(List<String> actives);

        void openofflineMap();

    }
}
